package com.moyu.oasys.announcement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moyu.oasys.announcement.mapper.AnnouncementMapper;
import com.moyu.oasys.announcement.entity.Announcement;
import com.moyu.oasys.announcement.service.IAnnouncementService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author 赵彦博
 * @Date 2024/12/2 19:48
 */
@Service
public class AnnouncementServiceImpl implements IAnnouncementService {

    private final AnnouncementMapper announcementMapper;

    public AnnouncementServiceImpl(AnnouncementMapper announcementDao) {
        this.announcementMapper = announcementDao;
    }

    @Override
    public PageInfo<Announcement> list(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return PageInfo.of(announcementMapper.list());
    }

    @Override
    public Announcement create(Announcement announcement) {
        LocalDateTime now = LocalDateTime.now();
        announcement.setCreateTime(now);
        announcement.setUpdateTime(now);
        announcementMapper.insert(announcement);
        return announcement;
    }

    @Override
    public Announcement update(Announcement announcement) {
        announcement.setUpdateTime(LocalDateTime.now());
        announcementMapper.update(announcement);
        return announcementMapper.selectById(announcement.getId());
    }

    @Override
    public void delete(Long id) {
        announcementMapper.deleteById(id);
    }

}
